<?php
/*
 *  权限
 */

namespace app\adminbackstage\controller;
use think\facade\Session;
use think\facade\Request;

use app\adminbackstage\model\RoleUser;
use app\adminbackstage\model\Menu;
use app\adminbackstage\model\Role;
use app\adminbackstage\model\Role_menu;
use app\adminbackstage\model\User;
use app\adminbackstage\model\Button;
/**
 * Description of Index
 *
 * @author asus80
 */
class JurisdictionController {
    //权限
    public function jurisdiction(){
        
        $uid = Request::param('uid');

        $role_user = new RoleUser();
        $menu = new Menu();
        $user = new User();
        $where_u[] = ["u.uid",'=',$uid];
        $find = $user->find($where_u);
        if(empty($find)){
            return json(['code' => -200, 'msg' => lang('无菜单权限'), 'data' => '', 'count' => '']);
        }
        else{
            $find = $find->toArray();
   
            if($find['enable']!=1){
                return json(['code' => -200, 'msg' => lang('无菜单权限'), 'data' => '', 'count' => '']);
            }
        }

        if($uid != 1){
            $where[] = ['m.enable','=',1];
//            $where[] = ['m.jur','=',1];
            $where[] = ['r.enable','=',1];
//            $where[] = ['b.enable','=',1];
            $where[] = ['me.type','=',1];
//            $where[] = ['m.mid','=',21];
            $where[] = ['r.uid','=',$uid];
            $listinfo = $role_user->jurisdiction($where)->toArray();
            $jurisdiction = array();

            if(!empty($listinfo)){
         
                foreach ($listinfo as $key=>$val){
                    if(empty($jurisdiction[$val['mid']])  && $val['jur']==1 && $val['mbid']==0){
                          // bid, b_name, b_iden,mid,rid,uid, jur, superior_id , menu_name, link, Icon, exhibition, component, sort
                        $jurisdiction[$val['mid']] = array('menu_name'=>$val['menu_name'],
                             'superior_id'=>$val['superior_id'],
                             'link'=>$val['link'],
                             'exhibition'=>$val['exhibition'],
                             'component'=>$val['component'],
                             'sort'=>$val['sort'],
                             'jur'=>$val['jur'],
                             'rid'=>$val['rid'],
                             'mid'=>$val['mid'],
                             'Icon'=>$val['Icon'],
                             'button'=>array()
                        );
                        /*if($val['benable']==1){
                            array_push($jurisdiction[$val['mid']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                        }*/
                        
                    }
                    /*elseif(!empty($jurisdiction[$val['mid']])  && $val['jur']==1 && $val['mbid']!=0){
                        if($val['benable']==1){
                            array_push($jurisdiction[$val['mid']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                        }
                    }*/
                }
                
                foreach ($listinfo as $key=>$val){
                    if(!empty($jurisdiction[$val['mid']])  && $val['jur']==1 && $val['mbid']!=0){
                        if($val['benable']==1){
                            array_push($jurisdiction[$val['mid']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                        }
                    }
                }
                
                $where_all[] = ['enable','=',1];
                $where_all[] = ['superior_id','=',0];
                $where_all[] = ['type','=',1];
                $menuAll = $menu->menuAll($where_all)->toArray();
                
                foreach($menuAll as $k=>$v){

                    foreach ($jurisdiction as $key_=>$val_){
                        
                        if($val_['superior_id'] == $v['id']){
                            //$jurisdiction[$val_['superior_id']] = array("id"=>$v['id'],"superior_id"=>"0","menu_name"=>$v['menu_name'],"link"=>$v['link'],"component"=>$v['component'],"Icon"=>$v['Icon'],"exhibition"=>$v['exhibition'],"btnPermissions"=>array(),"sort"=>$v["sort"]);
                            $jurisdiction[$val_['superior_id']] = array('menu_name'=>$v['menu_name'],
                             'superior_id'=>0,
                             'link'=>$v['link'],
                             'exhibition'=>$v['exhibition'],
                             'component'=>$v['component'],
                             'sort'=>$v['sort'],
                             'jur'=>'',
                             'rid'=>'',
                             'mid'=>$v['id'],
                             'Icon'=>$v['Icon'],
                             'button'=>array()
                             );
                            //$jurisdiction[$val_['superior_id']] = array("id"=>$v['meid'],"superior_id"=>"0","menu_name"=>$v['menu_name'],"link"=>$v['link'],"component"=>$v['component'],"Icon"=>$v['Icon'],"exhibition"=>$v['exhibition'],"btnPermissions"=>array($v['r_read'],$v['r_writ']),"sort"=>$v["sort"]);
                        }
                    }
                }
                
            }
            else{
                return json(['code' => 0, 'msg' => lang('无菜单权限'), 'data' => '', 'count' => '']);
            }
        }
        else{
            //admin 获取所有菜单
            $where = [];
            $where[] = ['me.enable','=',1];

            $where[] = ['me.type','=',1];
            $listinfo = $role_user->adminJurisdiction($where);
             
            if(!empty($listinfo)){
                $listinfo = $listinfo->toArray();
            }
            else{
                return json(['code' => 0, 'msg' => lang('无菜单'), 'data' => '', 'count' => '']);
            }
            $jurisdiction = array();
            
            foreach ($listinfo as $key=>$val){
                if($val['superior_id']!=0){
      
                    if(empty($jurisdiction[$val['id']]) ){
                    // superior_id , menu_name,link,Icon,exhibition, component, sort, b_name, b_iden,b.mid
                        $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                             'superior_id'=>$val['superior_id'],
                             'link'=>$val['link'],
                             'exhibition'=>$val['exhibition'],
                             'component'=>$val['component'],
                             'sort'=>$val['sort'],
                             'jur'=>1,
                             'rid'=>'',
                             'mid'=>$val['id'],
                             'Icon'=>$val['Icon'],
                             'button'=>array()
                        );
                        /*if(!empty($val['b_name'])){
                            array_push($jurisdiction[$val['id']]['button'],array('bid'=>'','b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                        }*/
                        
                    }
                    /*else{
                        if(!empty($val['b_name'])){
                            array_push($jurisdiction[$val['id']]['button'],array('bid'=>'','b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                        }
                    }*/
                }
                else{
                    $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                     'superior_id'=>0,
                     'link'=>$val['link'],
                     'exhibition'=>$val['exhibition'],
                     'component'=>$val['component'],
                     'sort'=>$val['sort'],
                     'jur'=>1,
                     'rid'=>'',
                     'mid'=>$val['id'],
                     'Icon'=>$val['Icon'],
                     'button'=>array()
                     );
                }

            }
            
            foreach ($listinfo as $key=>$val){
                if(!empty($val['b_name'])){
                    array_push($jurisdiction[$val['id']]['button'],array('bid'=>'','b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                }
            }

        }    
        
        $return = array();
        $a = 0;
        foreach ($jurisdiction as $key=>$val){
            $return[$a] = $val;
            $a++;
        }
    
        return json(['code' => 1, 'msg' => lang(''), 'data' =>  $return, 'count' => '']);
        
    }
    
    
    //添加角色
    public function roleAdd(){
        $role = new Role();
        $a_uid = Request::param('a_uid');
        $name = Request::param('name');
        $enable = Request::param('enable');
        $type = Request::param('type');
        $stid = Request::param('stid');//门店ID
        if(empty($enable)){
            $enable = 1;
        }
        
        if(empty($stid)){
            $stid  = 0;
        }
        
        if(empty($type)){
            $type  = 1;
        }
        
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [

            'name' => $name,
            'enable' => $enable,
            'addtime' => $time,
            'stid' => $stid,
            'type' => $type
        ];
        $role->roleAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    
    //角色修改
    public function roleEdit(){
        $role = new Role();
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $name = Request::param('name');
        $enable = Request::param('enable');
        $type = Request::param('type');
        $stid = Request::param('stid');//门店ID
        if(empty($stid)){
            $stid  = 0;
        }
        
        if(empty($type)){
            $type  = 1;
        }
        if($enable == -1){
            $enable = 0;
        }
        $update = ['name' => $name,'enable' => $enable];

        $role->roleEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    //角色列表
    public function roleList(){
        $role = new Role();
        $page = Request::param('page');
        $num = Request::param('num');
        $a_uid = Request::param('a_uid');
        $type = Request::param('type');
        $stid = Request::param('stid');//门店ID
        if(empty($stid)){
            $stid  = 0;
        }
        
        if(empty($type)){
            $type  = 1;
        }
      
        $where = [];
        $where[] = ['type','=',$type];
        $where[] = ['stid','=',$stid];
        $list = $role->listRole($where,$page,$num);
        foreach ($list as $key => $value) {
            $list[$key]["addtime"] = date( "Y-m-d H:i:s",$list[$key]["addtime"]);
        }
        $count = $role->countRole($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' =>$list, 'count' => $count]);
    }
    
    //菜单列表
    public function menulist(){
        $page = Request::param('page');
        $num = Request::param('num');
        $enable = Request::param('enable');
        $superior_id = Request::param('superior_id');
        $menu_name = Request::param('menu_name');
        $a_uid = Request::param('a_uid');
        $type = Request::param('type');
        $menu = new Menu();
        $where_all = [];
        if(!empty($superior_id)){
            if($superior_id == -1){
                $superior_id = 0;
                $page = 1;
                $num = 500;
            }
            $where[] = ['superior_id','=',$superior_id];
            $where_all[] = ['me.superior_id','=',$superior_id];
        }
        
        if(!empty($enable)){
            $where[] = ['enable','=',$enable];
            $where_all[] = ['me.enable','=',$enable];
        }
        if(!empty($menu_name)){
            $where[] = ['menu_name','like','%'.$menu_name.'%'];
            $where_all[] = ['me.menu_name','like','%'.$menu_name.'%'];
        }
        if(empty($type)){
            $type=1;
        }
        $where[] = ['type','=',$type];
        $where_all[] = ['me.type','=',$type];

        $listinfo = $menu->menulist($page,$num,$where_all);
        if(!empty($listinfo)){
            $listinfo = $listinfo->toArray();
        }
        else{
            return json(['code' => 0, 'msg' => lang('无菜单'), 'data' => '', 'count' => '']);
        }

//        $listinfo = $menu->menulist($page,$num,$where);
        $count = 0;
        $count = $menu->menuCount($where); 
        $jurisdiction = array();

        foreach ($listinfo as $key=>$val){
            if($val['superior_id']!=0){

                if(empty($jurisdiction[$val['id']]) ){
                // superior_id , menu_name,link,Icon,exhibition, component, sort, b_name, b_iden,b.mid
                    $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                         'superior_id'=>$val['superior_id'],
                         'link'=>$val['link'],
                         'exhibition'=>$val['exhibition'],
                         'component'=>$val['component'],
                         'sort'=>$val['sort'],
                         'jur'=>1,
                         'rid'=>'',
                         'mid'=>$val['id'],
                         'Icon'=>$val['Icon'],
                         'enable'=>$val['enable'],
                         'button'=>array()
                    );
                    if(!empty($val['b_name'])){
                        array_push($jurisdiction[$val['id']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                    }

                }
                else{
                    if(!empty($val['b_name'])){
                        array_push($jurisdiction[$val['id']]['button'],array('bid'=>$val['bid'],'b_name'=>$val['b_name'],'b_iden'=>$val['b_iden']));
                    }
                }
            }
            else{
                $jurisdiction[$val['id']] = array('menu_name'=>$val['menu_name'],
                 'superior_id'=>0,
                 'link'=>$val['link'],
                 'exhibition'=>$val['exhibition'],
                 'component'=>$val['component'],
                 'sort'=>$val['sort'],
                 'jur'=>'',
                 'rid'=>'',
                 'mid'=>$val['id'],
                 'Icon'=>$val['Icon'],
                 'enable'=>$val['enable'],
                 'button'=>array()
                 );
            }
        }

        $return = array();
        $a = 0;
        foreach ($jurisdiction as $key=>$val){
            $return[$a] = $val;
            $a++;
        }
        
        return json(['code' => 1, 'msg' => lang(''), 'data' =>  $return, 'count' => $count]);
    }
    
    
    
    //菜单添加
    public function menuAdd(){
        $menu = new Menu();
        $a_uid = Request::param('a_uid');
        $superior_id = Request::param('superior_id');
        $menu_name = Request::param('menu_name');
        $link = Request::param('link');
        $Icon = Request::param('Icon');
        $component = Request::param('component');
        $exhibition = Request::param('exhibition');
        $enable = Request::param('enable');
        $sort = Request::param('sort');
        $type = Request::param('type');
        
        $time = time();
        if(empty($superior_id) || $superior_id == -1){
            $superior_id = 0;
        }
        if(empty($exhibition) || $exhibition == -1){
            $exhibition = 0;
        }
        
        if($enable == -1){
            $enable = 0;
        }
        
        $data = [
            'superior_id' => $superior_id,
            'menu_name' => $menu_name ,
            'link' => $link,
            'Icon' => $Icon,
            'exhibition' => $exhibition,
            'component' => $component,
            'enable' => $enable,
            'sort' => $sort,
            'addtime' => $time,
            'type' => $type,
        ];
        $menu->menuAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    
    //菜单修改
    public function menuEdit(){
        $menu = new Menu();
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $superior_id = Request::param('superior_id');
        $menu_name = Request::param('menu_name');
        $component = Request::param('component');
        $link = Request::param('link');
        $Icon = Request::param('Icon');
        $exhibition = Request::param('exhibition');
        $enable = Request::param('enable');
        $sort = Request::param('sort');
        $type = Request::param('type');
        
        if(empty($superior_id) || $superior_id == -1){
            $superior_id = 0;
        }
        if(empty($exhibition) || $exhibition == -1){
            $exhibition = 0;
        }
        if($enable == -1){
            $enable = 0;
        }
        if(!empty($menu_name)){
            $update = [
                'superior_id' => $superior_id,
                'menu_name' => $menu_name ,
                'link' => $link,
                'component' => $component,
                'Icon' => $Icon,
                'exhibition' => $exhibition,
                'sort' => $sort,
                'enable' => $enable,
                'type' => $type,
            ];
        }
        else{
            $update = [

                'enable' => $enable,
            ];
        }
        
        $menu->menuEdit($id,$update);

        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    
    //按钮添加
    public function buttonAdd(){
        $button = new Button();
        $a_uid = Request::param('a_uid');
        $mid = Request::param('mid');
        $b_name = Request::param('b_name');
        $b_iden = Request::param('b_iden');
        $enable = Request::param('enable');
 
        $time = time();
        if($enable == -1){
            $enable = 0;
        }
        else{
            $enable = 1;
        }
        
        $data = [

            'mid' => $mid,
            'b_name' => $b_name,
            'b_iden' => $b_iden,
            'enable' => $enable,
            'addtime' => $time,
        ];
        $button->buttonAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    //按钮修改
    public function buttonEdit(){
        $button = new Button();
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $mid = Request::param('mid');
        $b_name = Request::param('b_name');
        $b_iden = Request::param('b_iden');
        $enable = Request::param('enable');

        if($enable == -1){
            $enable = 0;
        }
        if(!empty($b_name)){
            $update = [
                'mid' => $mid,
                'b_name' => $b_name,
                'b_iden' => $b_iden,
                'enable' => $enable,
            ];
        }
        else{
            $update = [
                'enable' => $enable,
            ];
        }
        
        $button->buttonEdit($id,$update);

        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    //按钮查询
    public function buttonList(){
        $button = new Button();
        $a_uid = Request::param('a_uid');
        $mid = Request::param('mid');
        $page = Request::param('page');
        $num = Request::param('num');
        $where = [];
        if(!empty($mid)){
            $where[] = ['b.mid','=',$mid];
        }
        $where[] = ['b.id','>','0'];
//        $where[] = ['enable','=','1'];
        $return = $button->buttonList($where,$page,$num);
        if(!empty($return)){
            $return = $return->toArray();
            $count = $button->buttonCount($where);
            return json(['code' => 1, 'msg' => lang(''), 'data' =>$return, 'count' => $count]);
        }
        return json(['code' => 0, 'msg' => lang('无数据'), 'data' =>'', 'count' => '']);
    }
    
    
    
    //角色权限查询
    public function rmList(){
        $role_menu = new Role_menu();
        $a_uid = Request::param('a_uid');
        
        $rid = Request::param('rid');
   
        $where = [];
        $where[] = ['rm.rid','=',$rid];
        $where[] = ['rm.enable','=','1'];
        
        $return = $role_menu->rmList($where)->toArray();

        if(!empty($return)){

            return json(['code' => 1, 'msg' => lang(''), 'data' =>$return, 'count' => '']);
        }
        return json(['code' => 1, 'msg' => lang('无数据'), 'data' =>'', 'count' => '']);
        
    }
    
    
    //角色权限修改
    public function rmEdit(){
        $role_menu = new Role_menu();
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        
        $rid = Request::param('rid');

        $jur = Request::param('jur');//6-1-1-2 mid-bid-jur(1：有 2：无)
        //$jur ="15-0-2,12-0-2,12-8-1,12-9-1,12-10-1,13-0-2,13-12-1,16-0-2,16-11-1,17-0-2";
        $jur = explode(",", $jur);
        $time = time();
        $update_p = ['enable'=>'0'];
        $role_menu->rmEditE($rid,$update_p);
        
        foreach ($jur as $value) {
            $strarr = explode("-", $value);
            
            $mid = $strarr[0];
            $bid = $strarr[1];
            $jur = $strarr[2];
            
            $where = [];
            $where[] = ['rm.rid','=',$rid];
            $where[] = ['rm.mid','=',$mid];
            $where[] = ['rm.bid','=',$bid];
//            $where[] = ['jur','=',$jur];
//            $where[] = ['enable','=','1'];
            
            $return = $role_menu->rmList($where)->toArray();
            if(!empty($return)){
                $update = ['rid' => $rid,'mid' => $mid,'bid' => $bid,'jur' => $jur,'enable'=>'1'];
                $id = $return[0]['id'];
                $role_menu->rmEdit($id,$update);
            }
            else{
                $data = [
                    'rid' => $rid,
                    'mid' => $mid,
                    'bid' => $bid,
                    'jue' => $jur,
                    'addtime' => $time,
                    'enable' => 1
                ];
                $role_menu->rmAdd($data);
            }
        }
        
        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
    
        
    //角色用户查询
    public function ruList(){
        $roleuser = new RoleUser();
        $a_uid = Request::param('a_uid');
        
//        $rid = Request::param('rid');
        $uid = Request::param('uid');

        $type = Request::param('type');
        $stid = Request::param('stid');
        
        if(empty($type)){
            $type = 1;
        }
        if(empty($stid)){
            $stid = 0;
        }
        $where = [];
//        $where[] = ['rid','=',$rid];
        $where[] = ['ru.uid','=',$uid];
        $where[] = ['ru.enable','=','1'];
        $where[] = ['r.type','=',$type];
        $where[] = ['r.stid','=',$stid];
        $return = $roleuser->ruList($where);
        if(!empty($return)){
            $return = $return->toArray();
            return json(['code' => 1, 'msg' => lang(''), 'data' =>$return, 'count' => '']);
        }
        return json(['code' => 0, 'msg' => lang('无数据'), 'data' =>'', 'count' => '']);
        
    }
    
    
    //角色用户修改
    public function ruEdit(){
        $roleuser = new RoleUser();
        $a_uid = Request::param('a_uid');
        if($a_uid != 1 ){
            //return json(['code' => 0, 'msg' => lang('该账号无操作权限'), 'data' =>'', 'count' => '']);
        }
        
        $rid = Request::param('rid');
        $uid = Request::param('uid');
        $where[] = ['ru.uid','=',$uid];
        $time = time();
        $where[] = ['ru.enable','=','1'];
        $return = $roleuser->ruList($where)->toArray();
        if(!empty($return)){

            
            $update = ['rid' => $rid,'uid' => $uid];
            $id = $return[0]['id'];
            $roleuser->ruEdit($id,$update);
        }
        else{
            $data = [
                'rid' => $rid,
                'uid' => $uid,

                'addtime' => $time,
                'enable' => 1
            ];
            $roleuser->ruAdd($data);
        }
        
        return json(['code' => 1, 'msg' => lang(''), 'data' =>'', 'count' => '']);
    }
    
}
